package com.mj.myvirtualapp.hooktest;

import android.util.Log;
import android.util.LongSparseArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.nio.charset.Charset;

public class Test {

    public static void main(String[] args) {


        for (int i = 0; i < 255; i++) {
            final int num=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Process exec = Runtime.getRuntime().exec("ping 192.168.253." + num);
                        BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(new String(line.getBytes(), Charset.forName("gbk")) + "  " + num);
                        }


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        }

        if (true)
            return;


//        ClassLoader classLoader=new ClassLoader(null,Test.class.getClass().getClassLoader()) ;
//        IMyAidlInterface
        // 先获取到当前的ActivityThread对象
        Class<?> activityThreadClass = null;
        try {
            activityThreadClass = Class.forName("com.mj.test.hooktest.SingleTonTest");
            Class<?> aClass = Test.class.getClassLoader().loadClass("com.mj.test.hooktest.SingleTonTest");
            Method getInstance1 = aClass.getDeclaredMethod("getInstance");
            getInstance1.setAccessible(true);
            Object invoke = getInstance1.invoke(null);
            System.out.println("classloader " + invoke + "  " + invoke.getClass().getClassLoader());

            Method getInstance = activityThreadClass.getDeclaredMethod("getInstance");
            getInstance.setAccessible(true);
            Object obj = getInstance.invoke(null);

            SingleTonTest instance = SingleTonTest.getInstance();

            Method getRandom = activityThreadClass.getDeclaredMethod("getRandom");
            getRandom.setAccessible(true);
            Object num = getRandom.invoke(instance);

            System.out.println(obj);
            System.out.println(instance);


            System.out.println(num);
            System.out.println(instance.getRandom());

            Field ourInstance = activityThreadClass.getDeclaredField("ourInstance");
            ourInstance.setAccessible(true);
            Object o1 = ourInstance.get(null);
            Object o2 = ourInstance.get(instance);

            Field random = activityThreadClass.getDeclaredField("random");
            random.setAccessible(true);
            Constructor<?> declaredConstructor = activityThreadClass.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);
            Object r1 = random.get(declaredConstructor.newInstance());
            Object r2 = random.get(instance);

            System.out.println(o1 + "  " + o2);
            System.out.println(r1 + "  " + r2);

        } catch (Exception e) {
            e.printStackTrace();
        }

        TestInterface proxyInstance = (TestInterface) Proxy.newProxyInstance(TestInterface.class.getClassLoader(), new Class[]{TestInterface.class}, new InvocationHandler() {

            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("testing");
//
                System.out.println(method.getName());
                Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
                System.out.println(declaredAnnotations.length);
                //返回到方法上
                return "返回值";
            }
        });
        System.out.println("before test");
        Object test = proxyInstance.test();
        System.out.println("before after " + test);
        LongSparseArray array;
//        Executors.newCachedThreadPool();
//        Executors.newScheduledThreadPool();
//        LruCache
        Log.e("q", "d");
//        ArrayList
    }


}
